/* 
 * This class implements a table that displays component names and their
 * concentrations at various points during a titration.
 *
 * Author: Aaron A. Lovato
 */
package gui;
 
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.*;

public class TrialResultTableModel extends AbstractTableModel
{
    protected ResourceBundle resbundle;
    private String[] columns;
    private Object[][] data;

    public TrialResultTableModel(String[] components)
    {
        // The ResourceBundle below contains all of the strings used in this application. 
        // New localities can be added by adding additional properties files.
        resbundle = ResourceBundle.getBundle ("strings", Locale.getDefault());
        columns = new String[components.length + 1];
        columns[0] = resbundle.getString("volume");
        //Copy the component names
        for(int index = 0; index < components.length; index++)
        {
            columns[index+1] = components[index];
        }
        
        data = new Object[1][columns.length];
        data[0][0] = new String();
        //Probably should initialize the first row based on an argument
        for(int index = 0; index < columns.length; index++)
        {
            data[0][index] = new Double(0.0);
        }
    }
    
    public int getColumnCount()
    {
        return columns.length;
    }
    
    public int getRowCount()
    {
        return data.length;
    }
    
    public String getColumnName(int col) 
    {
        return columns[col];
    }

    public Object getValueAt(int row, int col) 
    {
        return data[row][col];
    }
    
    public Class getColumnClass(int c) 
    {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) 
    {
        return false;
    }

    public void setValueAt(Object value, int row, int col) 
    {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
    
    public void addRow(Object[] row)
    {
        int newLength = data.length + 1;
        Object[][] newData = new Object[newLength][columns.length];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
        for(int index = 0; index < columns.length; index++)
        {
            data[newLength-1][index] = row[index];
        }
        fireTableRowsInserted(data.length, data.length);
    }
}
